package xyz.kongbai121.shop.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import xyz.kongbai121.shop.order.vo.PayVo;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000118626716";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCHkeB73qMYz+9uNFUTYIEK1AzQNg0WdGm6+M5xqh+zxMoQGoufoxztVXSGJsIMSW7PD4N45vuszD99Y9Ba59YaWV6cbGsmlZt90e9PIJ4I69JzdO2YHM38Y+AU6AuTRfyYCx6FCHgOdbJtqjVCmYtV9BI9J7cDg0TPXpMOlfYnHuzaSbJ4SY9zW2fpSl7q6gls5Ay6YA8/m0CbePHVGc+XZ/1akWLNOTr3S1Zn0UmbH2mqXaIjZofCbvlMnv/cluewweLdyfeNtHJm+60yHn1gEQ5Fvw5y4uARFUNxeN1kbCLagzIasogFAlxMSqcossV/ja/rrROjQoGoOBH7Vy+rAgMBAAECggEAOQVdYxxggzYqYbL3OaA0hoZsIUMqvBAYzICL7Hj+vpD94EoaAfoMV7LCwQ4aPirAxayjwr18i9CYv7e00heDETAXwDlF1EPG0+PyANkaP2xY5yD+MiWjag3kGUcEX1xngtfibVgDjyln9AJMRofoeC2hYkGLE5op3gLMXa0rjIYzbVuKhdAgF9PWnkLnShr8FE0zYs97oonj3GgYR1Gd2J5j1FQK7lfi0qUVmIkwVGeTxKmNWUyhVz4zCbVRmnD2HtE6DX8HsCvN6aJVztpdApjtuVqVviX9PeOVHZ2jm5mvZMHvFEzCPj73PewmGIsozfrtnnh0A17zh8uIIxmBoQKBgQDEqEkmt2x3WXkDrhxLl+rcpS3XbXXco7GW0VhYNGrnRK8NTx54PMLyMeHB6KYtehRwDSjjQ/DzVJ5l/yzSzRtmcgvZmn9JY/eOVwNxUQOESxy4E3RfsZNA4iAkNuymOgL73M1OYdm2FXoJrY8ofT5fP7Mr/hNUAFJkSTNBi+/JUQKBgQCwepmZXXK1+T/+EluwVR747WfYoh3D2bRZ8fz34QaHt1Pq0/WcnR9DIQDIIKL4kCey1aZ5zkmeKb6Jg8W/gvjZIZe5YwGsIvaETHH5k6FldC9yRNN0FNnw2wzB+YV9UYjlU62zJe0Iep/CSiaabC3VHjVwrnTm6dYX1D/yMCSqOwKBgQDBAi1xWzqQVHcJ3psUshAm59L7CQpRNzN3wxUC1MdLHcT0uf88oN3wGHbq+mwGXDes1a2Yh7j3CMjBcFVMu1M2E55/vjtJC10PDWVqlMlv0Mxp+WYQ+VZ9qrFC+cj/SBZfnRC+6hhjteZlkzra0TkjAIDYH3BW1E4I8TLmFPKNUQKBgAwbXQ44avYw80Lq339pV4acQtOJy6Dz/nttLn2S4m/NE+S4RdFItixUx0jzdDq5KQALyC/zBvniwz+L2rUYtxmuREpjkGnOvDq3Q9prVlcvcVHVhuwfwhPT8zwbchz2yv1wkCoKIAjE4NDjZWKSqVJFkET6supkgFuCJCDpTJqhAoGBAJ0tz5QWen0wjaioWyZ+VRqgXL8bM209jpnpQsaZEZ+ObPLRY2rQZss7G1Pk0w5KvuyhcXwUdt/WEonjaTWqSxhyFDlRpuFgwtthhlkP/PKcCio5wF2YVkW/61z7aQ2mhVsOtWnW+EDkNlwgodhGtEuv34qo9QGIsPp7KGJ6dPeQ";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnRxrNsGFLY3h4prem9NRJORHiX1pg58b7VesQfHLMn/NdMm8LMlRzp8Zg9b/aoLJEFRrJwRznB1cJykQ/EyXE3kcWNfowNF2/FGJHEDCy4+ZAdTsr+Y5zqJvElW4cNsdAe49gSk+OaSeqWxF0X4d3dV9xyyHjsAF3BR1JE6LlrcWYdc8f07HFHXjzCGYSHTsyB1T+QBVV/N9a/NBzGOUdkBs2JV3XIXpCMJ6cI3cTTDBCD7lxAvBcAo32PanFhD1qrO+DLCuRMsfrYc4SMIUwW043h9nxDZ/UklimkMNjZO69LO79yMBpwYbxhQquHrL65SV/4Xkwz6eNshAnmvrOwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url;

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
//    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
